#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>




void * thread_worker1(void *args) ;
void * thread_worker2(void *args) ;


int main(int argc, char **argv)
{
	int		shared_var = 1000 ;
	pthread_t	tid ;
	pthread_attr_t	thread_attr ;

	if ( pthread_attr_init(&thread_attr) != 0 )
	{
		printf("pthread_attr_init() failed: %s\n", strerror(errno)) ;
		return -1 ;
	}

	if ( pthread_attr_setstacksize( &thread_attr, 1024*120) != 0 )
	{
		printf("pthread_setstacksize() failure: %s\n", strerror(errno) ) ;
		return -2 ;

	}

	if ( pthread_attr_setdetachstate(&thread_attr,  PTHREAD_CREATE_DETACHED) != 0 )
	{
		printf("pthread_attr_setdetachstate() error: %s\n", strerror(errno)) ;
		return -3 ;
	}

	pthread_create(&tid, &thread_attr,  thread_worker1, &shared_var);
	printf("Thread worker1 tid[%ld] created ok\n", tid) ;

	pthread_create(&tid, NULL, thread_worker2, &shared_var) ;
	printf("Thread worker2 tid[%ld] created ok\n", tid) ;


	if ( pthread_attr_destroy(&thread_attr) != 0 )
	{
		printf("pthread_attr_destroy() failed :%s\n", strerror(errno)) ;
		return -4 ;
	}

	/*	Waiting until thread_worker2 exit()	*/
	 pthread_join(tid, NULL);


	while(1)
	{
		printf("Main/Control thread shared_var: %d\n", shared_var) ;
		sleep(10) ;
	}

	return 0 ;
}

void *thread_worker1(void *args)
{
	int	*ptr = (int *)args ;

	if(!args)
	{
		printf("%s() get invalid arguments\n", __FUNCTION__) ; // __FUNCTION__ get function name
		pthread_exit(NULL) ;
	}
	
	printf("Thread worker1 [%ld] start running...\n", pthread_self()) ;

	while(1)
	{
		printf("+++: %s before shared_var++: %d\n", __FUNCTION__, *ptr) ;
		*ptr += 1 ;
		sleep(2) ;
		printf("+++: %s after sleep shared_var: %d\n", __FUNCTION__, *ptr) ;
	}

	printf("Thread worker1 exit...\n") ;

	return NULL ;

}

void *thread_worker2(void *args)
{
	int	*ptr = (int *)args ;

	if(!args)
	{
		printf("%s() get invalid arguments\n", __FUNCTION__) ; // _FUNCTION_ get function name !!左右两个下划线
		pthread_exit(NULL) ;
	}
	
	printf("Thread worker2 [%ld] start running...\n", pthread_self()) ;

	while(1)
	{
		printf("---: %s before shared_var++: %d\n", __FUNCTION__, *ptr) ;
		*ptr += 1 ;
		sleep(2) ;
		printf("---: %s after sleep shared_var: %d\n", __FUNCTION__, *ptr) ;
	}

	printf("Thread worker2 exit...\n") ;

	return NULL ;

}
